from flask import Flask,request,redirect,jsonify,make_response,render_template



app = Flask(__name__)
username_list1=[]
login_list=[]

@app.route('/',methods=['POST','GET'])
def registered():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        if username not in username_list1:
            username_list1.append({'username':username,'password':password})
            print("注册成功",username_list1)
            return redirect('/login/')
        else:
            return make_response("用户已存在")
    return render_template('registered.html')



@app.route('/login/',methods=['POST','GET'])
def login():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        if username not in [i['username'] for i in username_list1]:
            return "用户不存在"
        else:
            for i in username_list1:
                if username == i['username'] and password == i['password']:
                    login_list.append({'username':username,'password':password})
                    print('登陆成功',login_list)
                    return redirect('/home')
            return "账号或密码错误"
    return render_template('login.html')



@app.route('/home',methods=['POST','GET'])
def home():
    if request.method == 'POST':
        password = request.form.get('password')

        login_list[-1]['password'] = password
        print("修改成功",login_list[-1])
        return jsonify(login_list[-1])
    return render_template('home.html')


@app.route('/image/',methods=['POST','GET'])
def image():
    file = request.files.get('name')
    image_name = file.filename
    resp = make_response(image_name)
    resp.headers['Content-Type'] = 'image/png'
    return resp



if __name__ == '__main__':
    app.run()



